/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-24
*
\*********************************************************************/

#ifndef PROTOCOL_MCU_H
#define PROTOCOL_MCU_H

#include "Communicate.h"

#ifdef MCU_IS_ALIVE
#include <vector>
#include <mutex>
#include "ReportFocusCtlDelegate.h"
#ifdef MCU_COMM_TYPE_IS_QT
#include <QObject>
#include "ProtocolBaseQt.h"
class ProtocolMCU : public QObject, public ProtocolBaseQt
{
    Q_OBJECT
#else
#include "ProtocolBasePosix.h"
class ProtocolMCU : public ProtocolBasePosix
{
#endif //#ifdef MCU_COMM_TYPE_IS_QT
public:
    static ProtocolMCU& GetInstance() {return m_Instance;}//获取实例
    ProtocolMCU& operator = (const ProtocolMCU&) = delete;
    ProtocolMCU(const ProtocolMCU&) = delete;
private:
    static ProtocolMCU m_Instance;
    ProtocolMCU() {}
public:
    void ExtractData(const unsigned char* data, int nLength) override;
    int CycleSendOnce() override;
    void MotoSelfCheck(ReportFocusCtlDelegate *ptr);//相机启动自检
    void MotoSelfCheck();//相机启动自检
    bool GetSelfCheckState();//true表示自检正常 false表示自检异常
//    bool GetSelfCheckResult();
    void IncFocus(ReportFocusCtlDelegate *ptr, unsigned char step = 10);//增大相机焦距，步长step，最大150
    void IncFocus(unsigned char step = 10);//增大相机焦距，步长step，最大150
    void DecFocus(ReportFocusCtlDelegate *ptr, unsigned char step = 10);//减小相机焦距，步长step，最小50
    void DecFocus(unsigned char step = 10);//减小相机焦距，步长step，最小50
    void SetFocus(unsigned char value, ReportFocusCtlDelegate *ptr);//设置相机焦距，范围50～150
    void SetFocus(unsigned char value);//设置相机焦距，范围50～150
    void SetView(float value);//设置相机视场角，范围6.9～20.4
    unsigned char GetFocus();
    float GetViewField();

    void SetWorkMode(bool mode);//设置相机工作状态

    void GetFocusCommand();//发送获取当前焦距指令

private:
    void EventReceiveFrameParse();
    void CycleReceiveFrameParse();
    void DefaultSend();
    void FocusDefaultSend();
    int SendFrame();
    void BuildFrame();
    void SelfCheckParse();
    void FoucsParse();
    void WorkModeParse();
    void FocusNowConfirm(unsigned char focus);

    void ReportSelfCheck(bool result);//向上位机或者吊舱反馈自检操作结果
    void ReportFocusInc(bool result);//向上位机或者吊舱反馈增益增加操作结果
    void ReportFocusDec(bool result);//向上位机或者吊舱反馈增益减小操作结果
    void ReportFocusSet(bool result);//向上位机或者吊舱反馈增益设置操作结果

private:
    const static unsigned char HEAD1 = 0x67;
    const static unsigned char HEAD2_EVENT = 0x00;
    const static unsigned char HEAD2_CYCLE = 0x01;
    const static unsigned char END = 0xFB;
    const static int FRAME_LENGTH_EVENT_SEND = 7;
    const static int FRAME_LENGTH_EVENT_RECEIVE = 8;
    const static int FRAME_LENGTH_CYCLE_RECEIVE = 4;
    const static unsigned char SUCCESS = 0x01;
    const static unsigned char FAIL = 0x02;
    const static unsigned char FOCUS_MAX = 150;
    const static unsigned char FOCUS_MIN = 50;

    unsigned char m_header1Send;
    unsigned char m_header2Send;
    unsigned char m_funcCodeSend;
    unsigned char m_commandSend;
//    unsigned char m_data1Send;
    unsigned char m_dataSend;
    unsigned char m_endSend;//帧尾
    unsigned char m_frameToSend[FRAME_LENGTH_EVENT_SEND];

    unsigned char m_funcCodeReceive;
    unsigned char m_commandReceive;
//    unsigned char m_data1Receive;
    unsigned char m_dataReceive;
    unsigned char m_resultReceive;
    bool m_result = false;

    unsigned char m_focusNow = 150;
    std::mutex m_focusMtx;

    bool m_selfCheckState = true;//true表示自检正常 false表示自检异常
//    bool m_selfCheckResult = true;//false表示自检失败、true表示自检成功
    ReportFocusCtlDelegate *m_reportDelegate = nullptr;


};

#endif // PROTOCOL_MCU_H
#endif //#ifdef MCU_IS_ALIVE

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/








